package com.xiaomengkuan.test.service.impl;

import com.xiaomengkuan.test.dao.*;
import com.xiaomengkuan.test.entity.pojo.*;
import com.xiaomengkuan.test.entity.vo.ProductTagsVo;
import com.xiaomengkuan.test.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-07 1:51 下午, File Encoding: utf8 --
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductRepository productRepository;
    @Resource
    TagRepository tagRepository;
    @Resource
    TagProductRelationRepository tagProductRelationRepository;
    @Resource
    ProductMainPictureRepository productMainPictureRepository;
    @Resource
    ProductDetailPictureRepository productDetailPictureRepository;
    @Resource
    ProductStoryRepository productStoryRepository;

    @Override
    public List<ProductTagsVo> getBySort() {
        List<Product> all = productRepository.findAll();
        List<ProductTagsVo> collect = all.stream().map((entity) -> {
            ProductTagsVo productTagsVo = new ProductTagsVo();
            BeanUtils.copyProperties(entity, productTagsVo);
            int productId = productTagsVo.getId();
            List<TagProductRelation> allByProductId = tagProductRelationRepository.findAllByProductId(productId);
            if (all != null && allByProductId.size() > 0) {
                List<Tag> tags = allByProductId.stream().map(obj -> {
                    Tag one = tagRepository.getOne(obj.getTagId());
                    return one;
                }).collect(Collectors.toList());
                if (tags == null) {
                    tags = new ArrayList<>();
                }
                productTagsVo.setTags(tags);
            }
            productTagsVo.setLiked(false);
            productTagsVo.setFavourited(false);
//            log.error("{}", productTagsVo);
            return productTagsVo;
        }).sorted((obj1, obj2) -> {
            return obj1.getSort() - obj2.getSort();
        }).collect(Collectors.toList());
//        log.debug("collect={}", collect);
        return collect;
    }

    @Override
    public ProductTagsVo getOneById(int id) {
        Optional<Product> productObj = productRepository.findById(id);
        // 设置标签
        List<TagProductRelation> relations = tagProductRelationRepository.findAllByProductId(id);
        List<Tag> tags = relations.stream().map(entity -> {
            Optional<Tag> tag = tagRepository.findById(entity.getTagId());
            Tag tag1 = tag.get();
            return tag1;
        }).collect(Collectors.toList());
        ProductTagsVo vo = new ProductTagsVo();
        BeanUtils.copyProperties(productObj.get(), vo);
        vo.setTags(tags);

        // 设置商品故事
        List<ProductStory> stories = productStoryRepository.findAllByProductId(id);
        if (stories.size() > 0) {
            vo.setStories(stories);
        }

        // 设置商品主图
        List<ProductMainPicture> mainPictures = productMainPictureRepository.findAllByProductId(id);
        vo.setProductMainPictures(mainPictures);
        // 设置商品细节图
        List<ProductDetailPicture> detailPictures = productDetailPictureRepository.findAllByProductId(id);
        if (detailPictures == null || detailPictures.size() <= 0) {
            return vo;
        }
        if (detailPictures.size() >= 2 && detailPictures.size() < 5) {
            vo.setProductDetailPictures(new ArrayList<>());
            for (int i = 0; i < 2; i++) {
                vo.getProductDetailPictures().add(detailPictures.get(i));
            }
        } else if (detailPictures.size() >= 5 && detailPictures.size() < 8) {
            vo.setProductDetailPictures(new ArrayList<>());
            for (int i = 0; i < 5; i++) {
                vo.getProductDetailPictures().add(detailPictures.get(i));
            }
        } else if (detailPictures.size() >= 8) {
            vo.setProductDetailPictures(new ArrayList<>());
            for (int i = 0; i < 8; i++) {
                vo.getProductDetailPictures().add(detailPictures.get(i));
            }
        } else {
            vo.setProductDetailPictures(detailPictures);
        }
//        vo.setProductDetailPictures(detailPictures);

        return vo;
    }

    @Override
    public List<ProductDetailPicture> getAllDetailPictures(int id) {
        return productDetailPictureRepository.findAllByProductId(id);
    }
}
